# coding: utf8
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import os.path as osp
import time
import numpy as np
import math
import cv2
import argparse
# from paddlex import transforms
import paddlex as pdx
# from paddleocr import PaddleOCR

# 读数后处理中有把圆形表盘转成矩形的操作，矩形的宽即为圆形的外周长
# 因此要求表盘图像大小为固定大小，这里设置为[512, 512
METER_SHAPE = [512 * 2, 512 * 2]  # 高x宽
# 圆形表盘的中心点
CIRCLE_CENTER = [256 * 2, 256 * 2]  # 高x宽
# 圆形表盘的半径
CIRCLE_RADIUS = 250 * 2
# 圆周率
PI = 3.1415926536
# 在把圆形表盘转成矩形后矩形的高
# 当前设置值约为半径的一半，原因是：圆形表盘的中心区域除了指针根部就是背景了
# 我们只需要把外围的刻度、指针的尖部保存下来就可以定位出指针指向的刻度
# RECTANGLE_HEIGHT = 120
RECTANGLE_HEIGHT = 160 * 2
# RECTANGLE_HEIGHT = 250
# 矩形表盘的宽，即圆形表盘的外周长
# RECTANGLE_WIDTH = 1570
RECTANGLE_WIDTH = 1570 * 2
# 两种表盘的配置信息，包含每根刻度的值，量程，单位

##
# theta = 0
# rho = 0
# y = 0
# x = 0
METER_CONFIG = [{
    'scale_interval_value': 25.0 / 50.0,
    'range': 25.0,
    'unit': "(MPa)"
}, {
    'scale_interval_value': 1.6 / 32.0,
    'range': 1.6,
    'unit': "(MPa)"
}, {
    'scale_interval_value': 1 / 50,
    'range': 1,
    'unit': "(MPa)"
}, {
    'scale_interval_value': 10 / 10,
    'range': 10,
    'unit': "(MPa)"
}, {
    'scale_interval_value': (0.6 - 0.35) / 25,
    'range': 0.6,
    'unit': "(MPa)"
}]

# current name of meter
current_meter = ''
# file results
file_results = ''

# 表记数据
meter_data = {
    'scale_interval_value': 0,
    'range': 0,
    'unit': ''
}

# 表记数据映射关系
meter_data_mapping = {
    "DoublePointer_OilTemperatureMeter_3": {"scale_interval_value": 120 / 60, "range": 120, "unit": "(MPa)"},
    "DoublePointer_OilTemperatureMeter_1": {"scale_interval_value": 150 / 75, "range": 150, "unit": "(MPa)"},
    "DoublePointer_OilTemperatureMeter_2": {"scale_interval_value": 160 / 80, "range": 160, "unit": "(MPa)"},
    "DoublePointer_OilTemperatureMeter_4": {"scale_interval_value": 160 / 80, "range": 160, "unit": "(MPa)"},
    "SF6_1": {"scale_interval_value": 1 / 50, "range": 1, "unit": "(MPa)"},
    "StressMeter_2": {"scale_interval_value": 0.25 / 25, "range": 0.25, "unit": "(MPa)"},
    "1-JCQ3A-(3.0)": {"scale_interval_value": 3.0 / 30, "range": 3.0, "unit": "(MPa)"},
    "DoublePointer_OilTemperatureMeter_s": {"scale_interval_value": 160.0 / 80.0, "range": 160.0, "unit": "(MPa)"},
    "11-JCQ-10_800A(5.0)": {"scale_interval_value": 3.0 / 30, "range": 3, "unit": "(MPa)"},
}

# 分割模型预测类别id与类别名的对应关系
SEG_CNAME2CLSID = {'_background_': 0, 'maxPointer': 1, 'pointer': 2, 'scale': 3}


def parse_args():
    parser = argparse.ArgumentParser(description='Meter Reader Infering')
    parser.add_argument(
        '--det_model_dir',
        dest='det_model_dir',
        help='The directory of the detection model',
        type=str,
        default=r"G:\zzuliWorkSpace\DATASET\det\version02\best_model")
    parser.add_argument(
        '--seg_model_dir',
        dest='seg_model_dir',
        help='The directory of the segmentation model',
        type=str,
        default=r"G:\zzuliWorkSpace\DATASET\SEG\version010_z\best_model")
    parser.add_argument(
        '--imageNet_model_dir',
        dest='imageNet_model_dir',
        help='The directory of the imageNet model',
        type=str,
        default=r"G:\zzuliWorkSpace\MODEL\CLASS\v1\mode_lcalss_type")
    parser.add_argument(
        '--image_dir',
        dest='image_dir',
        help='The directory of images to be inferred',
        type=str,
        # default=None)
        # default='./test/1')
        default=r'D:\IT\pythonCode\ImageConversion\MyTest\img\test')
    parser.add_argument(
        '--image',
        dest='image',
        help='The image to be inferred',
        type=str,
        default=None, )
    # default=r'./test/1/ipc_20211108_100449_001.jpg')
    parser.add_argument(
        '--use_erode',
        dest='use_erode',
        help='Whether erode the lable map predicted from a segmentation model',
        action='store_true')
    parser.add_argument(
        '--erode_kernel',
        dest='erode_kernel',
        help='Erode kernel size',
        type=int,
        default=4)
    # default=0)
    parser.add_argument(
        '--save_dir',
        dest='save_dir',
        help='The directory for saving the predicted results',
        type=str,
        default='./output/result')
    parser.add_argument(
        '--score_threshold',
        dest='score_threshold',
        help="Predicted bounding boxes whose scores are lower than this threshlod are filtered",
        type=float,
        # default=0.3)
        default=0.5)
    parser.add_argument(
        '--seg_batch_size',
        dest='seg_batch_size',
        help="The number of images fed into the segmentation model during one forward propagation",
        type=int,
        default=1)

    return parser.parse_args()


def is_pic(img_name):
    """判断是否是图片

    参数：
        img_name (str): 图片路径

    返回：
        flag (bool): 判断值。
    """
    valid_suffix = ['JPEG', 'jpeg', 'JPG', 'jpg', 'BMP', 'bmp', 'PNG', 'png']
    suffix = img_name.split('.')[-1]
    flag = True
    if suffix not in valid_suffix:
        flag = False
    return flag

class MeterReader:
    """检测表盘的位置，分割各表盘内刻度和指针的位置，并根据分割结果计算出各表盘的读数

    参数：
        det_model_dir (str): 用于定位表盘的检测模型所在路径。
        seg_model_dir (str): 用于分割刻度和指针的分割模型所在路径。

    """

    def __init__(self, det_model_dir, seg_model_dir, imageNet_model_dir):
        if not osp.exists(det_model_dir):
            raise Exception("Model path {} does not exist".format(
                det_model_dir))
        if not osp.exists(seg_model_dir):
            raise Exception("Model path {} does not exist".format(
                seg_model_dir))
        self.detector = pdx.load_model(det_model_dir)
        self.segmenter = pdx.load_model(seg_model_dir)
        self.imageNet = pdx.load_model(imageNet_model_dir)
        # self.ocr_model = PaddleOCR(rec_model_dir='../PaddleOCR/inference/det_db',
        #                            rec_char_dict_path='../PaddleOCR/ppocr/utils/num_dict.txt',
        #                            rec_char_type='en',
        #                            max_text_length='5',
        #                            )

    def decode(self, img_file):
        """图像解码

        参数：
            img_file (str|np.array): 图像路径，或者是已解码的BGR图像数组。

        返回：
            img (np.array): BGR图像数组。
        """

        if isinstance(img_file, str):
            img = cv2.imread(img_file).astype('float32')
        else:
            img = img_file.copy()
        return img

    def filter_bboxes(self, det_results, score_threshold):
        """过滤置信度低于阈值的检测框

        参数：
            det_results (list[dict]): 检测模型预测接口的返回值。
            score_threshold (float)：置信度阈值。

        返回：
            filtered_results (list[dict]): 过滤后的检测狂。

        """
        filtered_results = list()
        for res in det_results:
            if res['score'] > score_threshold:
                filtered_results.append(res)
        return filtered_results

    def roi_crop(self, img, det_results):
        """抠取图像上各检测框的图像区域

        参数：
            img (np.array)：BRG图像数组。
            det_results (list[dict]): 检测模型预测接口的返回值。

        返回：
            sub_imgs (list[np.array]): 各检测框的图像区域。

        """
        sub_imgs = []
        i = 1
        for res in det_results:
            # Crop the bbox area
            xmin, ymin, w, h = res['bbox']
            xmin = max(0, int(xmin))
            ymin = max(0, int(ymin))
            xmax = min(img.shape[1], int(xmin + w - 1))
            ymax = min(img.shape[0], int(ymin + h - 1))
            sub_img = img[ymin:(ymax + 1), xmin:(xmax + 1), :]
            sub_imgs.append(sub_img)

        return sub_imgs

    def resize(self, imgs, target_size, interp=cv2.INTER_LINEAR):
        """图像缩放至固定大小

        参数：
            imgs (list[np.array])：批量BGR图像数组。
            target_size (list|tuple)：缩放后的图像大小，格式为[高, 宽]。
            interp (int)：图像差值方法。默认值为cv2.INTER_LINEAR。

        返回：
            resized_imgs (list[np.array])：缩放后的批量BGR图像数组。

        """

        resized_imgs = list()
        for img in imgs:
            img_shape = img.shape
            scale_x = float(target_size[1]) / float(img_shape[1])
            scale_y = float(target_size[0]) / float(img_shape[0])
            resize_img = cv2.resize(
                img, None, None, fx=scale_x, fy=scale_y, interpolation=interp)
            resized_imgs.append(resize_img)
        return resized_imgs

    def seg_predict(self, segmenter, imgs, batch_size):
        """分割模型完成预测

        参数：
            segmenter (pdx.seg.model)：加载后的分割模型。
            imgs (list[np.array])：待预测的输入BGR图像数组。
            batch_size (int): 分割模型前向预测一次时输入图像的批量大小。

        返回：
            seg_results (list[dict]): 输入图像的预测结果。

        """
        seg_results = list()
        num_imgs = len(imgs)
        for i in range(0, num_imgs, batch_size):
            batch = imgs[i:min(num_imgs, i + batch_size)]
            result = segmenter.predict(batch)
            seg_results.extend(result)
            # 强制为1，排除数字区
            break

        # 语义绘图
        result = segmenter.predict(imgs[0])
        ret, binary = cv2.threshold(seg_results[0]['label_map'], 0, 255, cv2.THRESH_BINARY)
        cv2.imwrite("yuyi.jpg", binary)
        pdx.seg.visualize(imgs[0], result, save_dir='./')
        return seg_results

    def image_predict(self, imageNet, imgs, batch_size):
        global meter_data
        global current_meter
        global CIRCLE_CENTER
        global CIRCLE_RADIUS
        global RECTANGLE_WIDTH
        global RECTANGLE_HEIGHT
        imageNet_results = list()
        num_imgs = len(imgs)
        for i in range(0, num_imgs, batch_size):
            batch = imgs[i:min(num_imgs, i + batch_size)]
            result = imageNet.predict(batch)
            imageNet_results.extend(result)

        # meter_data["scale_interval_value"] = meter_data_mapping[imageNet_results[0][0]["category"]][
        #     "scale_interval_value"]
        meter_data["scale_interval_value"] = meter_data_mapping[imageNet_results[0][0]["category"]][
            "scale_interval_value"]
        meter_data["range"] = meter_data_mapping[imageNet_results[0][0]["category"]][
            "range"]
        meter_data["unit"] = meter_data_mapping[imageNet_results[0][0]["category"]][
            "unit"]

        # 标记当前执行表记
        current_meter = str(imageNet_results[0][0]["category"])
        print(current_meter)

        # if current_meter == 'SF6':
        #     CIRCLE_CENTER[0] = 512
        #     CIRCLE_CENTER[1] = 512
        #     CIRCLE_RADIUS = 500
        #     RECTANGLE_HEIGHT = 300
        #     RECTANGLE_WIDTH = 1500
        if current_meter == '1-JCQ3A-(3.0)':
            CIRCLE_CENTER[0] = 1231
            CIRCLE_CENTER[1] = 412
            CIRCLE_RADIUS = 750
            RECTANGLE_HEIGHT = 100
            RECTANGLE_WIDTH = 1600
        if current_meter == 'DoublePointer_OilTemperatureMeter_4':
            CIRCLE_CENTER[0] = 498
            CIRCLE_CENTER[1] = 531
            CIRCLE_RADIUS = 750
            RECTANGLE_HEIGHT = 300
            RECTANGLE_WIDTH = 2450
        if current_meter == 'DoublePointer_OilTemperatureMeter_s':
            CIRCLE_CENTER[0] = 498
            CIRCLE_CENTER[1] = 531
            CIRCLE_RADIUS = 750
            RECTANGLE_HEIGHT = 300
            RECTANGLE_WIDTH = 2450
        return imageNet_results

    def erode(self, seg_results, erode_kernel):
        """对分割模型预测结果中label_map做图像腐蚀操作

        参数：
            seg_results (list[dict])：分割模型的预测结果。
            erode_kernel (int): 图像腐蚀的卷积核的大小。

        返回：
            eroded_results (list[dict])：对label_map进行腐蚀后的分割模型预测结果。

        """
        kernel = np.ones((erode_kernel, erode_kernel), np.uint8)
        eroded_results = seg_results
        for i in range(len(seg_results)):
            eroded_results[i]['label_map'] = cv2.erode(
                seg_results[i]['label_map'], kernel)
        return eroded_results

    def circle_to_rectangle(self, seg_results):
        """将圆形表盘的预测结果label_map转换成矩形

        圆形到矩形的计算方法：
            因本案例中两种表盘的刻度起始值都在左下方，故以圆形的中心点为坐标原点，
            从-y轴开始逆时针计算极坐标到x-y坐标的对应关系：
              x = r + r * cos(theta)
              y = r - r * sin(theta)
            注意：
                1. 因为是从-y轴开始逆时针计算，所以r * sin(theta)前有负号。
                2. 还是因为从-y轴开始逆时针计算，所以矩形从上往下对应圆形从外到内，
                   可以想象把圆形从-y轴切开再往左右拉平时，圆形的外围是上面，內围在下面。

        参数：
            seg_results (list[dict])：分割模型的预测结果。

        返回值：
            rectangle_meters (list[np.array])：矩形表盘的预测结果label_map。

        """
        rectangle_meters = list()
        # rectangle_meter的大小已经由预先设置的全局变量RECTANGLE_HEIGHT, RECTANGLE_WIDTH决定
        centre, r = self.find_centre(seg_results[0]['label_map'])
        print(centre)
        width = int(np.pi * 2 * r)
        high = r
        for i, seg_result in enumerate(seg_results):
            label_map = seg_result['label_map']
            rectangle_meter = np.zeros(
                (high, width), dtype=np.uint8)
            for row in range(high):
                for col in range(width):
                    theta = PI * 2 * (col + 1) / width
                    # 矩形从上往下对应圆形从外到内
                    rho = r - row - 1
                    # 确定CIRCLE_CENTER的参数即可确定起始位置，逆时针旋转
                    # y = int(centre['y'] + rho * math.cos(theta) + 0.5)
                    # x = int(centre['x'] - rho * math.sin(theta) + 0.5)
                    y = int(centre['y'] + rho * math.cos(theta) + 0.5)
                    x = int(centre['x'] - rho * math.sin(theta) + 0.5)

                    if x < METER_SHAPE[1] and y < METER_SHAPE[0] and x >= 0 and y >= 0:
                        rectangle_meter[row, col] = label_map[y, x]

            rectangle_meters.append(rectangle_meter)
        ret, binary = cv2.threshold(rectangle_meters[0], 0, 255, cv2.THRESH_BINARY)
        try:
            cv2.imwrite("jvxing.jpg", binary)
        except:
            pass

        return rectangle_meters

    def rectangle_to_line(self, rectangle_meters):
        """从矩形表盘的预测结果中提取指针和刻度预测结果并沿高度方向压缩成线状格式。

        参数：
            rectangle_meters (list[np.array])：矩形表盘的预测结果label_map。

        返回：
            line_scales (list[np.array])：刻度的线状预测结果。
            line_pointers (list[np.array])：指针的线状预测结果。

        """
        line_scales = list()
        line_pointers = list()

        for rectangle_meter in rectangle_meters:
            height, width = rectangle_meter.shape[0:2]
            line_scale = np.zeros((width), dtype=np.uint8)
            line_pointer = np.zeros((width), dtype=np.uint8)
            #
            line_maxPointer = np.zeros((width), dtype=np.uint8)
            maxxpointer_num_sign = 0
            #
            for col in range(width):
                for row in range(height):
                    if rectangle_meter[row, col] == SEG_CNAME2CLSID['pointer']:
                        line_pointer[col] += 1
                    elif rectangle_meter[row, col] == SEG_CNAME2CLSID['maxPointer']:
                        line_maxPointer[col] += 1
                        maxxpointer_num_sign += 1
                    elif rectangle_meter[row, col] == SEG_CNAME2CLSID['scale']:
                        line_scale[col] += 1

            line_scales.append(line_scale)
            line_pointers.append(line_pointer)
            if maxxpointer_num_sign > 0:
                line_scales.append(line_scale)
                line_pointers.append(line_maxPointer)
        return line_scales, line_pointers

    def mean_binarization(self, data_list):
        """对图像进行均值二值化操作

        参数：
            data_list (list[np.array])：待二值化的批量数组。

        返回：
            binaried_data_list (list[np.array])：二值化后的批量数组。

        """
        batch_size = len(data_list)
        binaried_data_list = data_list
        for i in range(batch_size):
            mean_data = np.mean(data_list[i])
            width = data_list[i].shape[0]
            for col in range(width):
                if data_list[i][col] < mean_data:
                    binaried_data_list[i][col] = 0
                else:
                    binaried_data_list[i][col] = 1
        return binaried_data_list

    def locate_scale(self, line_scales):
        """在线状预测结果中找到每根刻度的中心位置

        参数：
            line_scales (list[np.array])：批量的二值化后的刻度线状预测结果。

        返回：
            scale_locations (list[list])：各图像中每根刻度的中心位置。

        """
        batch_size = len(line_scales)
        scale_locations = list()
        for i in range(batch_size):
            line_scale = line_scales[i]
            width = line_scale.shape[0]
            find_start = False
            one_scale_start = 0
            one_scale_end = 0
            locations = list()
            for j in range(width - 1):
                if line_scale[j] > 0 and line_scale[j + 1] > 0:
                    if find_start == False:
                        one_scale_start = j
                        find_start = True
                if find_start:
                    if line_scale[j] == 0 and line_scale[j + 1] == 0:
                        one_scale_end = j - 1
                        one_scale_location = (
                                                     one_scale_start + one_scale_end) / 2
                        locations.append(one_scale_location)
                        one_scale_start = 0
                        one_scale_end = 0
                        find_start = False
            scale_locations.append(locations)
        return scale_locations

    def locate_pointer(self, line_pointers):
        """在线状预测结果中找到指针的中心位置

        参数：
            line_scales (list[np.array])：批量的指针线状预测结果。

        返回：
            scale_locations (list[list])：各图像中指针的中心位置。

        """
        batch_size = len(line_pointers)
        pointer_locations = list()
        for i in range(batch_size):
            line_pointer = line_pointers[i]
            find_start = False
            pointer_start = 0
            pointer_end = 0
            location = 0
            width = line_pointer.shape[0]
            for j in range(width - 1):
                if line_pointer[j] > 0 and line_pointer[j + 1] > 0:
                    if find_start == False:
                        pointer_start = j
                        find_start = True
                if find_start:
                    if line_pointer[j] == 0 and line_pointer[j + 1] == 0:
                        pointer_end = j - 1
                        location = (pointer_start + pointer_end) / 2
                        find_start = False
                        break
            pointer_locations.append(location)
        # print(pointer_locations[0])
        # print(pointer_locations[1])
        return pointer_locations

    def get_relative_location(self, scale_locations, pointer_locations):
        """找到指针指向了第几根刻度

        参数：
            scale_locations (list[list])：批量的每根刻度的中心点位置。
            pointer_locations (list[list])：批量的指针的中心点位置。

        返回：
            pointed_scales (list[dict])：每个表的结果组成的list。每个表的结果由字典表示，
                字典有两个关键词：'num_scales'、'pointed_scale'，分别表示预测的刻度根数、
                预测的指针指向了第几根刻度。

        """

        pointed_scales = list()
        for scale_location, pointer_location in zip(scale_locations,
                                                    pointer_locations):
            num_scales = len(scale_location)
            print("lllllll")
            print(scale_location)
            pointed_scale = -1
            if num_scales > 0:
                # me
                adjust = 0
                top_num = 0
                end_num = 0
                # if len(scale_location) >= 3:
                #     interval = scale_location[2] - scale_location[1]
                for i in range(num_scales - 1):
                    # if (scale_location[i + 1] - scale_location[i]) > 4 * interval:
                    #     top_num = i + 1
                    #     end_num = num_scales - top_num
                    #     adjust = 1
                    if scale_location[i] <= pointer_location and pointer_location < scale_location[i + 1]:
                        pointed_scale = i + (pointer_location - scale_location[i]) / (
                                scale_location[i + 1] - scale_location[i] + 1e-05
                        ) + 1
                        print("#######:")
                        print(pointed_scale)
                if pointed_scale <= top_num and adjust == 1:
                    pointed_scale = pointed_scale + end_num
                else:
                    pointed_scale = pointed_scale - top_num
            result = {'num_scales': num_scales, 'pointed_scale': pointed_scale}
            # result = {'num_scales': num_scales, 'pointed_scale': pointed_scale, maxPointer_scale:}
            pointed_scales.append(result)
            print("&&&&&&&")
            print(result)
        return pointed_scales

    def calculate_reading(self, pointed_scales):
        """根据刻度的间隔值和指针指向的刻度根数计算表盘的读数
        """
        readings = list()
        batch_size = len(pointed_scales)
        for i in range(batch_size):
            pointed_scale = pointed_scales[i]
            print("pointed_scale:", end="")
            print(pointed_scale)
            # 刻度根数大于阈值的为第一种表盘
            # if pointed_scale['num_scales'] > TYPE_THRESHOLD:
            #     reading = pointed_scale['pointed_scale'] * METER_CONFIG[4][
            #         'scale_interval_value']
            # else:
            #     reading = pointed_scale['pointed_scale'] * METER_CONFIG[4][
            #         'scale_interval_value']

            reading = pointed_scale['pointed_scale'] * meter_data[
                'scale_interval_value'] - meter_data['scale_interval_value']
            # reading = pointed_scale['pointed_scale'] * meter_data[
            #     'scale_interval_value']
            print("----met测试：")
            print(meter_data['scale_interval_value'])
            if current_meter == "DoublePointer_OilTemperatureMeter_2":
                if reading < 20:
                    reading = 20 - reading
                else:
                    reading = reading - 20

            if current_meter == "SF6_1":
                if reading < 0.1:
                    reading = 0.1 - reading
                else:
                    reading = reading - 0.1

            if current_meter == "StressMeter_2":
                reading = reading + 0.35
            # me
            # reading = reading - 0.1 - 0.02
            # reading = reading + 0.35 - 0.01
            # reading = reading - 0.01
            #
            readings.append(reading)
            print("-----##指针读数：")
            print(reading)
        return readings

    def find_centre(self, label_map):
        """
        通过遍历计算动态获得圆心坐标和圆半径
        :param label_mp:语义分割中返回的结果中'labelme'对象
        :return: [centre,r]:[圆心坐标,圆半径],均以四舍五入转为整刑
        """
        point = {
            'a': {
                'x': 25645,
                'y': 0
            },
            'b': {
                'x': 0,
                'y': 45646
            },
            'c': {
                'x': 0,
                'y': 0
            },
        }
        for i in range(label_map.shape[0]):
            for j in range(label_map.shape[1]):
                if label_map[i][j] != 0:
                    if j < point['a']['x']:
                        point['a']['x'] = j
                        point['a']['y'] = i
                    if j > point['c']['x']:
                        point['c']['x'] = j
                        point['c']['y'] = i
                    if i < point['b']['y']:
                        point['b']['x'] = j
                        point['b']['y'] = i
        center = {
            'x': 0,
            'y': 0
        }
        r = 0
        try:
            pab = [(point['a']['x'] + point['b']['x']) / 2, (point['a']['y'] + point['b']['y']) / 2]
            pbc = [(point['b']['x'] + point['c']['x']) / 2, (point['b']['y'] + point['c']['y']) / 2]
            kab = (point['a']['y'] - point['b']['y']) / (point['a']['x'] - point['b']['x'])
            kab = -1 / kab
            bab = pab[1] - pab[0] * kab
            kbc = (point['b']['y'] - point['c']['y']) / (point['b']['x'] - point['c']['x'] + 0.001)

            kbc = -1 / kbc
            bbc = pbc[1] - pbc[0] * kbc

            center['x'] = (bbc - bab) / (kab - kbc)
            center['y'] = kab * center['x'] + bab
            r = math.sqrt((center['x'] - point['a']['x']) ** 2 + (center['y'] - point['a']['y']) ** 2)
            # 整形化
            center['x'] = int(center['x'] + 0.5)
            center['y'] = int(center['y'] + 0.5)
            r = int(r + 0.5)
        except:
            pass
        if center['x'] >= 3000 or center['y'] >= 3000:
            center['x'] = 512
            center['y'] = 512
            r = 500
        return [center, r+100]

    def get_meter_reading(self, seg_results):
        """对分割结果进行读数后处理得到各表盘的读数

        参数：
            seg_results (list[dict]): 分割模型的预测结果。

        返回：
            meter_readings (list[dcit]): 各表盘的读数。

        """
        rectangle_meters = self.circle_to_rectangle(seg_results)
        line_scales, line_pointers = self.rectangle_to_line(rectangle_meters)
        binaried_scales = self.mean_binarization(line_scales)
        binaried_pointers = self.mean_binarization(line_pointers)
        scale_locations = self.locate_scale(binaried_scales)
        pointer_locations = self.locate_pointer(binaried_pointers)
        print("pointer_locations: ", end="")
        print(pointer_locations)

        pointed_scales = self.get_relative_location(scale_locations,
                                                    pointer_locations)
        meter_readings = self.calculate_reading(pointed_scales)
        print("meter_readings:", end="")
        print(meter_readings)
        return meter_readings

    def print_meter_readings(self, meter_readings):
        """打印各表盘的读数

        参数：
            meter_readings (list[dict])：各表盘的读数
        """
        for i in range(len(meter_readings)):
            print("Meter {}: {}".format(i + 1, meter_readings[i]))

    def visualize(self, img, det_results, meter_readings, save_dir="./"):
        """可视化图像中各表盘的位置和读数

        参数：
            img (str|np.array): 图像路径，或者是已解码的BGR图像数组。
            det_results (dict): 检测模型的预测结果。
            meter_readings (list): 各表盘的读数。
            save_dir (str)：可视化后的图片保存路径。

        """
        global file_results
        img_save = "./output/result/"
        vis_results = list()
        for i, res in enumerate(det_results):
            # 将检测结果中的关键词`score`替换成读数，就可以调用pdx.det.visualize画图了
            res['score'] = meter_readings[i]
            vis_results.append(res)
            # 强制为1，排除数字区
            break
        # 检测结果可视化时会滤除score低于threshold的框，这里读数都是>=-1的，所以设置thresh=-1
        # pdx.det.visualize(img, vis_results, threshold=-1, save_dir=save_dir)
        print("wwwwwwww")
        print(len(meter_readings))
        img_save += time.strftime("%Y-%m-%d %H-%M-%S", time.localtime()) + "--"
        for i in range(len(meter_readings)):
            if i == len(meter_readings) - 1:
                img_save += "Meter{}--{}".format(i + 1, meter_readings[i])
            else:
                img_save += "Meter{}--{}".format(i + 1, meter_readings[i]) + " "
            # print("Meter {}: {}".format(i + 1, meter_readings[i]))
        # img_save += "--" + time.strftime("%Y-%m-%d %H-%M-%S", time.localtime()) + ".jpg"
        file_results = img_save + file_results + ".jpg"
        cv2.imwrite(str(file_results), img)
        file_results = ""
        print(file_results)

    def classifyImg(self, results):
        '''
        主要功能就是区别出结果集中的不同目标
        :param results: 经过滤过的预测结果
        :return: 返回结果结构{
            '名称1': list,
            '名称2': list,
        }
        ----
        list表示一类结果的内部信息 (此结构相当于传入参数的results结构,方便后续处理各类目标)
        '''
        res = {}
        for result in results:
            if result['category'] not in result:
                res[result['category']] = []
            res[result['category']].append(result)
        return res

    def get_num(self, img):
        ocr = self.ocr_model
        img_path = '../PaddleOCR/doc/imgs_num/IMG_20210729_123357_1.png'
        result = ocr.ocr(img_path, det=False)
        print(result[0][0])

        return result[0][0]

    def predict(self,
                img_file,
                save_dir='./',
                use_erode=True,
                erode_kernel=4,
                # erode_kernel=0,
                score_threshold=0.5,
                seg_batch_size=2):
        """检测图像中的表盘，而后分割出各表盘中的指针和刻度，对分割结果进行读数后处理后得到各表盘的读数。

        参数：
            img_file (str)：待预测的图片路径。
            save_dir (str): 可视化结果的保存路径。
            use_erode (bool, optional): 是否对分割预测结果做图像腐蚀。默认值：True。
            erode_kernel (int, optional): 图像腐蚀的卷积核大小。默认值: 4。
            score_threshold (float, optional): 用于滤除检测框的置信度阈值。默认值：0.5。
            seg_batch_size (int, optional)：分割模型前向推理一次时输入表盘图像的批量大小。默认值为：2。
        """
        global file_results

        img = self.decode(img_file)
        det_results = self.detector.predict(img)

        filtered_results = self.filter_bboxes(det_results, score_threshold)

        res = self.classifyImg(filtered_results)
        # num
        try:
            sub_nums = self.roi_crop(img, res['number'])
            sub_nums = self.resize(sub_nums, METER_SHAPE)
            cv2.imwrite("num.jpg", sub_nums[0])
            num = self.get_num(sub_nums[0])
            file_results = '--num--' + str(num)
            print("num=" + str(num))
        except Exception as e:
            pass
        # num

        sub_imgs = self.roi_crop(img, res['meter'])
        sub_imgs = self.resize(sub_imgs, METER_SHAPE)

        # 标记当前表记类型，不要删除！！
        imageNet_result = self.image_predict(self.imageNet, sub_imgs, seg_batch_size)
        seg_results = self.seg_predict(self.segmenter, sub_imgs, seg_batch_size)
        seg_results = self.erode(seg_results, erode_kernel)
        meter_readings = self.get_meter_reading(seg_results)
        self.print_meter_readings(meter_readings)
        self.visualize(img, filtered_results, meter_readings, save_dir)


def infer(args):
    image_lists = list()
    if args.image is not None:
        if not osp.exists(args.image):
            raise Exception("Image {} does not exist.".format(args.image))
        if not is_pic(args.image):
            raise Exception("{} is not a picture.".format(args.image))
        image_lists.append(args.image)
    elif args.image_dir is not None:
        if not osp.exists(args.image_dir):
            raise Exception("Directory {} does not exist.".format(
                args.image_dir))
        for im_file in os.listdir(args.image_dir):
            if not is_pic(im_file):
                continue
            im_file = osp.join(args.image_dir, im_file)
            image_lists.append(im_file)

    meter_reader = MeterReader(args.det_model_dir, args.seg_model_dir, args.imageNet_model_dir)
    if len(image_lists) > 0:
        for image in image_lists:
            meter_reader.predict(image, args.save_dir, args.use_erode,
                                 args.erode_kernel, args.score_threshold,
                                 args.seg_batch_size)


if __name__ == '__main__':
    args = parse_args()
    infer(args)
